package com.xoa.controller.h5AppLogin;

import com.xoa.dao.WeixinGzh.UserWeixinGzhMapMapper;
import com.xoa.model.WeixinGzh.UserWeixinGzhMap;
import com.xoa.model.WeixinGzh.UserWeixinGzhMapExample;
import com.xoa.model.common.SysPara;
import com.xoa.model.users.Users;
import com.xoa.service.dingdingManage.DingdingManageService;
import com.xoa.service.enterpriseWeChat.EnterpriseWeChatService;
import com.xoa.service.h5AppLogin.H5AppLoginService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dingding.DingDingTool;
import com.xoa.util.enterpriseWeChat.EnterpriseWeChatUrl;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * Created by 张雨 on 2018/3/5.
 * 钉钉微应用
 */
@Controller
@RequestMapping("/m")
public class H5AppLoginController {

    @Autowired
    DingdingManageService dingdingManageService;

    @Autowired
    EnterpriseWeChatService enterpriseWeChatService;

    @Autowired
    H5AppLoginService h5AppLoginService;

    @Autowired
    UserWeixinGzhMapMapper userWeixinGzhMapMapper;

    @RequestMapping("/lxloginh5")
    public String lxloginh5() {
        return "/login/m/lxloginh5";
    }

    @RequestMapping("/widgets")
    public String widgets() {
        return "/login/m/widget";
    }

    @RequestMapping("/getMainddh5")
    public String mainddh5(HttpServletRequest request, String openId) {

        //将当前登录的微信用户与OA用户绑定
        if(null != openId&&!openId.equals("null")) {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);

            if(user != null) {
                String uid = user.getUid()+"";
                String uidByWeixinId = userWeixinGzhMapMapper.getUidByWeixinId(openId);
                UserWeixinGzhMap userWeixinGzhMap = new UserWeixinGzhMap();
                userWeixinGzhMap.setWxUid(openId);
                userWeixinGzhMap.setOaUid(uid);
                //该微信号是否绑定oa账号
                if(null!=uidByWeixinId) {
                    //绑定的oa账号是否是当前账号
                    if(!uidByWeixinId.equals(uid)) {
                        //执行更新操作
                        UserWeixinGzhMapExample example = new UserWeixinGzhMapExample();
                        example.or().andWxUidEqualTo(openId);
                        userWeixinGzhMapMapper.updateByExample(userWeixinGzhMap, example);
                    }
                } else {
                    //未绑定oa账号执行插入操作
                    userWeixinGzhMapMapper.insertSelective(userWeixinGzhMap);
                }
            }
        }
        return "/app/main/m/mainddh5";
    }

    @RequestMapping("/getMainlxh5")
    public String mainlxh5() {
        return "/app/main/m/mainlxh5";
    }
    //微信公众号H5首页登录页面
    @RequestMapping("/getH5LoginPage")
    public String H5LoginPage(String code) {
        String result = "/login/m/H5LoginPage";
        //获取当前登陆的微信用户信息
        if(!StringUtils.checkNull(code)) {
            String appId = "wxebdebe207cfee590";
            String appSecret = "d95317f1e484e08c2d7fec142571dac6";
            String doRequest = "?appid=" + appId + "&secret=" + appSecret + "&code="+code+"&grant_type=authorization_code";
            String resultJson = DingDingTool.dingdingRequestGet(EnterpriseWeChatUrl.WECHAT_GET_OPENID, doRequest);
            String openId = DingDingTool.getMsgFromJson(resultJson, "openid");
            if(!StringUtils.checkNull(openId)) {
                result = "/login/m/H5LoginPage?openId="+openId;
            }
        }
        return result;
    }

    //钉钉微应用

    //朱洪飞
    //H5微应用钉钉单点登录获取code码
    @RequestMapping("/chooseTeam")
    public String chooseTeam(HttpServletRequest request) {
        //获取corpId和corpSecret
        BaseWrapper wrapper = dingdingManageService.baseDingDingGet();
        String corpId = "";
        String corpSecret = "";
        if(wrapper.isFlag()) {
            List<SysPara> sysParaList = (List<SysPara>) wrapper.getData();
            for(SysPara sysPara: sysParaList) {
                if(sysPara.getParaName().equals("DINGDINGQY_CORPID")) {
                    corpId = sysPara.getParaValue();
                }
                if(sysPara.getParaName().equals("DINGDINGQY_SECRET")) {
                    corpSecret = sysPara.getParaValue();
                }
            }
            request.setAttribute("corpId", corpId);
            request.setAttribute("corpSecret", corpSecret);
        }
        return "/login/m/ddloginh5";
    }

    //H5微应用企业微信单点登录获取code码
    @RequestMapping("/selectedOrganization")
    public String selectedOrganization(HttpServletRequest request) {
        //获取corpId和corpSecret
        BaseWrapper wrapper = enterpriseWeChatService.baseEnterpriseWeChatGet();
        String corpId = "";
        String corpSecret = "";
        if(wrapper.isFlag()) {
            List<SysPara> sysParaList = (List<SysPara>) wrapper.getData();
            for(SysPara sysPara: sysParaList) {
                if(sysPara.getParaName().equals("WECHATQY_CORPID")) {
                    corpId = sysPara.getParaValue();
                }
            }
            request.setAttribute("corpId", corpId);
        }
        return "/login/m/wxloginh5";
    }

    //钉钉免登码获取用户身份
    @ResponseBody
    @RequestMapping("/dingdingLoginH5")
    public BaseWrapper dingdingLoginH5(HttpServletRequest request,HttpServletResponse response, String corpId, String corpSecret, String code, String oId) throws JSONException {
        return h5AppLoginService.dingdingLoginH5(request, response, corpId, corpSecret, code, oId);
    }

    //蓝信单点登录
    @ResponseBody
    @RequestMapping("/lxloginh5api")
    public BaseWrapper lxloginh5api(HttpServletRequest request, HttpServletResponse response, String loginId) {
        return h5AppLoginService.lxloginh5api(request, response, loginId);
    }

    //企业微信免登码获取用户身份
    @ResponseBody
    @RequestMapping("/enterpriseWeChatLoginH5")
    public BaseWrapper enterpriseWeChatLoginH5(HttpServletRequest request,HttpServletResponse response, String auth_code) throws JSONException {
        return h5AppLoginService.enterpriseWeChatLoginH5(request, response, auth_code);
    }

}
